﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "aio_helper.h"
#include "aio_impl.h"
#include "aio_tcp.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 
namespace xos_aio
{
    aio_tcp::aio_tcp( aio_impl * pAioImpl ) : m_pAioImpl( pAioImpl )
    {
    }

    aio_tcp::~aio_tcp()
    {
    }

    // 
    // m_bData[0] : true or false for result
    // m_bData[1] : true or false for server
    // 
    // m_nData[0] : cmd type
    // 
    // m_nData[1] : local port
    // m_szStr[1] : local ip
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::tcp_init( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;
        void * lpUserKey = lpMsg->get_void( 0, 0 );
        bool bIsServer = lpMsg->get_bool( 1, 0 );

        SOCKET hSocket = INVALID_SOCKET;
        aio_key * lpAioKey = 0;
        xos::i_socket * lpSocket = 0;

        if( 0 == nRet )
        {
            lpMsg->set_int( 0, i_aio::AIO_TCP_INIT_RET );
            lpMsg->set_bool( 0, false );
            lpMsg->set_void( 1, 0 );
        }

        if( 0 == nRet )
        {
            nRet = g_pSysData->m_pContainer->xos()->create(xos::i_xos::XOS_OBJ_SOCKET, ( void ** )&lpSocket);
        }

        if( 0 == nRet )
        {
            xos::xos_u16 usPort = (xos::xos_u16)lpMsg->get_int( 1, 0 );
            const char * pszIp = lpMsg->get_str( 1, 0, 0, 0 );
            nRet = lpSocket->tcp_init( pszIp, usPort, true );
        }

        if( 0 == nRet )
        {
            lpSocket->set_async( true );
        }

        if( ( 0 == nRet ) && bIsServer )
        {
            lpSocket->tcp_listen( 5 );
        }

        if( 0 == nRet )
        {
            xos::xos_u16 usPort = 0;
            char szIp[100] = { 0 };
            nRet = lpSocket->get_socket_addr( szIp, &usPort );
            if( 0 == nRet )
            {
                lpMsg->set_str( 1, szIp );
                lpMsg->set_int( 1, usPort );
            }
        }

        if( 0 == nRet )
        {
            lpAioKey = aio_key::get_item_from_pool( true );
            lpAioKey->init();
            lpAioKey->m_eState = aio_key::KEY_STATE_RUNNING;
            lpAioKey->m_lpUserKey = lpUserKey;
            lpAioKey->m_pSocket = lpSocket;
            hSocket = (SOCKET)lpSocket->get_socket();
        }

        if( 0 == nRet )
        {
            HANDLE h = CreateIoCompletionPort( (HANDLE)hSocket, m_pAioImpl->m_hIocp, (ULONG_PTR)lpAioKey, 0 );
            if( h != m_pAioImpl->m_hIocp )
            {
                nRet = 1;
            }
        }

        if( 0 == nRet )
        {
            lpMsg->set_void( 1, lpAioKey );
            lpMsg->set_bool( 0, true );
            lpSocket = 0;
            lpAioKey = 0;
        }

        m_pAioImpl->notify( lpMsg );
        lpMsg = 0;

        if( lpSocket )
        {
            lpSocket->release();
            lpSocket = 0;
        }

        if( lpAioKey )
        {
            lpAioKey->m_pSocket = 0;
            lpAioKey->release();
            lpAioKey = 0;
        }

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // 
    // m_nData[1] : local port
    // m_szStr[1] : local ip
    // m_nData[2] : peer port
    // m_szStr[2] : peer ip
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // m_lpData[2] : ClientKey
    // 
    int aio_tcp::tcp_accept_ret( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;
        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        aio_key * lpClientKey = (aio_key *)lpMsg->get_void( 2, 0 );
        xos::i_socket * pClientSocket = lpClientKey->m_pSocket;
        xos::i_socket * lpSocket = lpAioKey->m_pSocket;
        xos_box::i_buf * lpBuf = lpMsg->get_buf( 0, 0 );

        --lpAioKey->m_nRequestNum;
        lpMsg->set_buf( 0, 0 );

        if( ( 0 != nResult ) || !lpMsg->get_bool( 0, 0 ) )
        {
            nRet = 1;
        }

        if( 0 == nRet )
        {
            DWORD dwS = sizeof( sockaddr_in );
            DWORD dwSize = dwS + 16;
            sockaddr_in * local = 0, * remote = 0;
            int l_size = dwS, r_size = dwS;
            char ip[100] = { 0 };
            m_pAioImpl->m_lpGetAcceptExSockaddrs( lpBuf->get_data( 0, 0, 0 ), 0, dwSize, dwSize, (LPSOCKADDR *)&local, &l_size, (LPSOCKADDR *)&remote, &r_size );
            //lpMsg->set_str( 1, inet_ntoa( local->sin_addr ) );
            lpMsg->set_str( 1, inet_ntop( AF_INET, (const void *)&local->sin_addr, ip, sizeof( ip ) ) );
            lpMsg->set_int( 1, ntohs( local->sin_port ) );
            //lpMsg->set_str( 2, inet_ntoa( remote->sin_addr ) );
            lpMsg->set_str( 2, inet_ntop( AF_INET, (const void *)&remote->sin_addr, ip, sizeof( ip ) ) );
            lpMsg->set_int( 2, ntohs( remote->sin_port ) );
        }

        if( 0 == nRet )
        {
            SOCKET hSocket = (SOCKET)lpSocket->get_socket();
            setsockopt( (SOCKET)pClientSocket->get_socket(),
                SOL_SOCKET,
                SO_UPDATE_CONNECT_CONTEXT,
                (char *)&hSocket,
                sizeof( hSocket ) );
        }

        if( 0 == nRet )
        {
            SOCKET hSocket = (SOCKET)pClientSocket->get_socket();
            if( m_pAioImpl->m_hIocp != CreateIoCompletionPort( (HANDLE)hSocket, m_pAioImpl->m_hIocp, (ULONG_PTR)lpClientKey, 0 ) )
            {
                nRet = 1;
            }
        }

        if( 0 == nRet )
        {
            lpMsg->set_bool( 0, true );
            lpClientKey = 0;
        }
        else
        {
            lpMsg->set_bool( 0, false );
            lpMsg->set_void( 2, 0 );
        }

        if( lpClientKey )
        {
            lpClientKey->release();
            lpClientKey = 0;
        }

        if( lpBuf )
        {
            lpBuf->release();
            lpBuf = 0;
        }

        m_pAioImpl->notify( lpMsg );

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // 
    // m_nData[1] : local port
    // m_szStr[1] : local ip
    // m_nData[2] : peer port
    // m_szStr[2] : peer ip
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // m_lpData[2] : ClientKey
    // 
    int aio_tcp::tcp_accept( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;
        void * lpUserKey = lpMsg->get_void( 0, 0 );

        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        xos::i_socket * lpServerSocket = lpAioKey->m_pSocket;
        xos::i_socket * lpClientSocket = 0;

        aio_data * lpRetData = 0;
        xos_box::i_buf * lpBuf = 0;
        aio_key * pKey = 0;

        if( 0 == nRet )
        {
            lpMsg->set_int( 0, i_aio::AIO_TCP_ACCPET_RET );
            lpMsg->set_bool( 0, false );
            lpMsg->set_void( 2, 0 );
            lpMsg->set_buf( 0, 0 );
            lpAioKey->m_nRequestNum++;
        }

        if( 0 == nRet )
        {
            nRet = g_pSysData->m_pContainer->box()->create( xos_box::enumObj::BOX_OBJ_BUF, (void **)&lpBuf );
            lpMsg->set_buf( 0, lpBuf );
        }

        if( 0 == nRet )
        {
            lpRetData = aio_data::get_item_from_pool( true );
            lpRetData->init();
            lpRetData->m_nType = lpMsg->get_int( 0, 0 );
            lpRetData->m_lpMsgData = lpMsg;
        }

        if( 0 == nRet )
        {
            g_pSysData->m_pContainer->xos()->create( xos::i_xos::XOS_OBJ_SOCKET, (void **)&lpClientSocket );
            nRet = lpClientSocket->tcp_init( 0, 0, false );
            if( 0 == nRet )
            {
                nRet = lpClientSocket->set_async( true );
            }
        }

        if( 0 == nRet )
        {
            pKey = aio_key::get_item_from_pool( true );
            pKey->init();
            pKey->m_lpUserKey = lpUserKey;
            pKey->m_pSocket = lpClientSocket;
            lpMsg->set_void( 2, pKey );
        }

        if( 0 == nRet )
        {
            SOCKET hClientSocket = (SOCKET)lpClientSocket->get_socket();
            SOCKET hSocket = (SOCKET)lpServerSocket->get_socket();
            DWORD dwSize = sizeof( sockaddr_in ) + 16;
            lpMsg->set_bool( 0, true );
            if( !m_pAioImpl->m_lpAcceptEx( hSocket, hClientSocket, lpBuf->get_data( 0, 0, 0 ), 0, dwSize, dwSize, &lpRetData->m_dwBytes, (WSAOVERLAPPED *)&lpRetData->m_ov_ex ) )
            {
                DWORD dwError = WSAGetLastError();
                if( ERROR_IO_PENDING != dwError )
                {
                    nRet = 1;
                }
            }
        }

        if( 0 != nRet )
        {
            lpMsg->set_bool( 0, false );
            PostQueuedCompletionStatus( m_pAioImpl->m_hIocp, 0, 0, (WSAOVERLAPPED *)&lpRetData->m_ov_ex );
        }

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // 
    // m_nData[1] : local port
    // m_szStr[1] : local ip
    // m_nData[2] : peer port
    // m_szStr[2] : peer ip
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::tcp_connect_ret( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;
        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        xos::i_socket * lpSocket = lpAioKey->m_pSocket;

        if( ( 0 != nResult ) || !lpMsg->get_bool( 0, 0 ) )
        {
            nRet = 1;
        }

        if( 0 == nRet )
        {
            xos::xos_u16 usLocalPort = 0;
            char szLocalIp[100] = { 0 };
            nRet = lpSocket->get_socket_addr( szLocalIp, &usLocalPort );
            if( 0 == nRet )
            {
                lpMsg->set_str( 1, szLocalIp );
                lpMsg->set_int( 1, usLocalPort );
            }
        }

        if( 0 == nRet )
        {
            lpMsg->set_bool( 0, true );
            lpAioKey = 0;
        }
        else
        {
            lpMsg->set_bool( 0, false );
            lpMsg->set_void( 1, 0 );
        }

        if( lpAioKey )
        {
            lpAioKey->release();
            lpAioKey = 0;
        }

        m_pAioImpl->notify( lpMsg );

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // 
    // m_nData[1] : local port
    // m_szStr[1] : local ip
    // m_nData[2] : peer port
    // m_szStr[2] : peer ip
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::tcp_connect( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;
        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        xos::i_socket * lpSocket = lpAioKey->m_pSocket;
        void * lpUserKey = lpAioKey->m_lpUserKey;
        SOCKET hSocket = (SOCKET)lpSocket->get_socket();
        aio_data * lpRetData = 0;

        lpMsg->set_int( 0, i_aio::AIO_TCP_CONNECT_RET );
        lpMsg->set_bool( 0, false );

        if( 0 == nRet )
        {
            lpRetData = aio_data::get_item_from_pool( true );
            lpRetData->init();
            lpRetData->m_nType = lpMsg->get_int( 0, 0 );
            lpRetData->m_lpMsgData = lpMsg;
        }

        if( 0 == nRet )
        {
            xos::xos_u16 usPort = (xos::xos_u16)lpMsg->get_int( 2, 0 );
            const char * pszIp = lpMsg->get_str( 2, 0, 0, 0 );
            DWORD dwIp = 0;
            // 只是用一下get_ip_by_name这个不影响状态的方法
            lpSocket->get_ip_by_name( pszIp, &dwIp, 0 );
            lpRetData->m_addr.sin_port = htons( usPort );
            lpRetData->m_addr.sin_addr.s_addr = dwIp;
            lpRetData->m_addr.sin_family = AF_INET;
        }

        if( 0 == nRet )
        {
            lpMsg->set_bool( 0, true );
            if( !m_pAioImpl->m_lpConnectEx( hSocket, (const sockaddr *)&lpRetData->m_addr, sizeof( sockaddr_in ), NULL, 0, NULL, (WSAOVERLAPPED *)&lpRetData->m_ov_ex ) )
            {
                DWORD dwErr = WSAGetLastError();
                if( ERROR_IO_PENDING != dwErr )
                {
                    nRet = 1;
                }
            }
        }

        if( 0 != nRet )
        {
            lpMsg->set_bool( 0, false );
            PostQueuedCompletionStatus( m_pAioImpl->m_hIocp, 0, 0, (WSAOVERLAPPED *)&lpRetData->m_ov_ex );
        }

        lpRetData = 0;

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // 
    // m_nData[1] : recv len
    // m_lpBuf[0] : recv buf
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::tcp_recv_ret( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        using namespace std;
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;
        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        xos_box::i_buf * lpBuf = lpMsg->get_buf( 0, 0 );

        --lpAioKey->m_nRequestNum;

        if( ( 0 != nResult ) || ( 0 == dwBytes ) || !lpMsg->get_bool( 0, 0 ) )
        {
            nRet = 1;
        }

        if( 0 == nRet )
        {
            lpMsg->set_bool( 0, true );
            lpMsg->set_int( 1, dwBytes );
            lpBuf->set_len( dwBytes );
        }
        else
        {
            lpMsg->set_bool( 0, false );
        }

        m_pAioImpl->notify( lpMsg );

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // 
    // m_nData[1] : recv len
    // m_lpBuf[0] : recv buf
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::tcp_recv( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;
        void * lpUserKey = lpMsg->get_void( 0, 0 );
        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        xos::i_socket * lpSocket = lpAioKey->m_pSocket;
        SOCKET hSocket = (SOCKET)lpSocket->get_socket();
        xos_box::i_buf * lpBuf = lpMsg->get_buf( 0, 0 );
        aio_data * lpRetData = 0;

        lpMsg->set_int( 0, i_aio::AIO_TCP_RECV_RET );
        lpMsg->set_bool( 0, false );
        lpAioKey->m_nRequestNum++;

        if( 0 == nRet )
        {
            lpRetData = aio_data::get_item_from_pool( true );
            lpRetData->init();
            lpRetData->m_nType = lpMsg->get_int( 0, 0 );
            lpRetData->m_lpMsgData = lpMsg;
        }

        if( 0 == nRet )
        {
            WSABUF * lpWsaBuf = lpBuf->get_wsa_buf( 0 );
            lpMsg->set_bool( 0, true );
            int ret = ::WSARecv( hSocket, lpWsaBuf, 1, &lpRetData->m_dwBytes, &lpRetData->m_dwFlags, (WSAOVERLAPPED *)&lpRetData->m_ov_ex, 0 );
            if( SOCKET_ERROR == ret )
            {
                DWORD dwErr = WSAGetLastError();
                if( WSA_IO_PENDING != dwErr )
                {
                    nRet = 1;
                }
            }
        }

        if( 0 != nRet )
        {
            lpMsg->set_bool( 0, false );
            PostQueuedCompletionStatus( m_pAioImpl->m_hIocp, 0, 0, (WSAOVERLAPPED *)&lpRetData->m_ov_ex );
        }

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // 
    // m_nData[1] : send bytes
    // m_lpBuf[0] : send buf
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::tcp_send_ret( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        using namespace std;
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;
        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        xos_box::i_buf * lpBuf = lpMsg->get_buf( 0, 0 );

        --lpAioKey->m_nRequestNum;

        if( ( 0 != nResult ) || ( 0 == dwBytes ) || !lpMsg->get_bool( 0, 0 ) )
        {
            nRet = 1;
        }

        if( 0 == nRet )
        {
            lpMsg->set_bool( 0, true );
            lpMsg->set_int( 1, dwBytes );
        }
        else
        {
            lpMsg->set_bool( 0, false );
        }

        m_pAioImpl->notify( lpMsg );

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // 
    // m_nData[1] : send bytes
    // m_lpBuf[0] : send buf
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::tcp_send( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;
        void * lpUserKey = lpMsg->get_void( 0, 0 );
        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        xos::i_socket * lpSocket = lpAioKey->m_pSocket;
        SOCKET hSocket = (SOCKET)lpSocket->get_socket();
        xos_box::i_buf * lpBuf = lpMsg->get_buf( 0, 0 );
        aio_data * lpRetData = 0;

        lpMsg->set_int( 0, i_aio::AIO_TCP_SEND_RET );
        lpMsg->set_bool( 0, false );
        lpAioKey->m_nRequestNum++;

        if( 0 == nRet )
        {
            lpRetData = aio_data::get_item_from_pool( true );
            lpRetData->init();
            lpRetData->m_nType = lpMsg->get_int( 0, 0 );
            lpRetData->m_lpMsgData = lpMsg;
        }

        if( 0 == nRet )
        {
            WSABUF * lpWsaBuf = lpBuf->get_wsa_buf( 0 );
            lpMsg->set_bool( 0, true );
            int ret = ::WSASend( hSocket, lpWsaBuf, 1, &lpRetData->m_dwBytes, lpRetData->m_dwFlags, (WSAOVERLAPPED *)&lpRetData->m_ov_ex, 0 );
            if( SOCKET_ERROR == ret )
            {
                DWORD dwErr = WSAGetLastError();
                if( WSA_IO_PENDING != dwErr )
                {
                    nRet = 1;
                }
            }
        }

        if( 0 != nRet )
        {
            lpMsg->set_bool( 0, false );
            PostQueuedCompletionStatus( m_pAioImpl->m_hIocp, 0, 0, (WSAOVERLAPPED *)&lpRetData->m_ov_ex );
        }

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::shut_down_ret( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;

        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        xos::i_socket * lpSocket = lpAioKey->m_pSocket;
        int how = lpMsg->get_int( 1, 0 );

        lpSocket->shut_down( how );

        m_pAioImpl->notify( lpMsg );
        lpMsg = 0;

        return nRet;
    }

    // 
    // m_nData[0] : cmd type
    // m_nData[1] : how
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::shut_down( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;

        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        xos::i_socket * lpSocket = lpAioKey->m_pSocket;
        int how = lpMsg->get_int( 1, 0 );

        lpMsg->set_int( 0, i_aio::AIO_TCP_SHUTDOWN_RET );
        lpMsg->set_bool( 0, true );
        lpAioKey->m_eState = aio_key::KEY_STATE_CLOSING;

        CancelIo( (HANDLE)lpSocket->get_socket() );

        {
            aio_helper obj( m_pAioImpl );
            obj.post_msg( lpMsg );
            lpMsg = 0;
        }

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::close_net_ret( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;
        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );

        if( 0 == lpAioKey->m_nRequestNum )
        {
            lpMsg->set_void( 1, 0 );
            m_pAioImpl->notify( lpMsg );
            lpMsg = 0;
            lpAioKey->release();
            lpAioKey = 0;
        }
        else
        {
            aio_helper obj( m_pAioImpl );
            obj.post_msg( lpMsg );
            lpMsg = 0;
        }

        return nRet;
    }

    // 
    // m_bData[0] : true or false for opt result
    // 
    // m_nData[0] : cmd type
    // m_nData[1] : linger.l_onoff
    // m_nData[2] : linger.l_linger
    // 
    // m_lpData[0] : UserKey
    // m_lpData[1] : AioKey
    // 
    int aio_tcp::close_net( int nResult, DWORD dwBytes, aio_key * lpKey, aio_data * lpData )
    {
        int nRet = 0;

        xos_box::i_msg * lpMsg = lpData->m_lpMsgData;

        aio_key * lpAioKey = (aio_key *)lpMsg->get_void( 1, 0 );
        int onoff = lpMsg->get_int( 1, 0 );
        int linger = lpMsg->get_int( 2, 0 );
        xos::i_socket * lpSocket = lpAioKey->m_pSocket;

        lpMsg->set_int( 0, i_aio::AIO_TCP_CLOSE_RET );
        lpMsg->set_bool( 0, true );
        lpAioKey->m_eState = aio_key::KEY_STATE_CLOSING;

        lpSocket->close( onoff, linger );

        {
            aio_helper obj( m_pAioImpl );
            obj.post_msg( lpMsg );
            lpMsg = 0;
        }

        return nRet;
    }

} // xos_aio
